                      package Gui;

import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.accessibility.AccessibleContext;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import javax.swing.JTextArea;
import javax.swing.JTextField;

import controller.CreateListControl;
import controller.CustomerControl;
import controller.MarketingTemplateControl;



import java.awt.SystemColor;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;




// TODO: Auto-generated Javadoc
/**
 * The Class CreateList.
 */
public class CreateList extends JPanel {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -6400283674228776823L;

	/** The Deal number label. */
	public static JLabel DealNumberLabel;
	
	/** The Deal number text field. */
	public static JTextField DealNumberTextField ;
	
	/** The Cancel button. */
	public static JButton CancelButton= new JButton("CANCEL");
	
	/** The confirmation lable. */
	public static JLabel confirmationLable=null;
	
	/** The lbl new label. */
	public static JLabel lblNewLabel = new JLabel(" ");
	
	/** The lst. */
	private CreateListControl lst;
	
	/** The cc. */
	private CustomerControl cc;
	
	/** The mtc. */
	private MarketingTemplateControl mtc;
	
	/** The tolist. */
	private String tolist;
	
	/** The todivide. */
	public static  String todivide=null;
	
	/** The Enter button. */
	public static JButton EnterButton = new JButton("ENTER");
	
	/** The back button. */
	public static JButton backButton = new JButton("Main Menu");
	
	/** The btncreate. */
	public static JButton btncreate= new JButton("next");
	
	/** The btn new button. */
	public static JButton btnNewButton= new JButton("next");
	
	/** The btndiv. */
	public static JButton btndiv= new JButton("next");
	
	/** The btnsend. */
	public static JButton btnsend= new JButton("next");
	
	/** The btn back. */
	public static JButton btnBack = new JButton("BACK");
	
	/** The text area_2. */
	public static JTextArea textArea_2;
	
	/** The text area_3. */
	public static JTextArea textArea_3;
	
	/** The text area_4. */
	public static JTextArea textArea_4;
	
	/** The text area_1. */
	public static JTextArea textArea_1;
    
    /** The mdg. */
    public MarketingDirectorGui mdg;
	
	/** The text. */
	public static String[] text;
	
	/** The liarr. */
	public static String[] liarr;
	
	/** The sublist. */
	public static String[] sublist;
	
	/** scrollpanel for customer list */
	public static JScrollPane scrollPane; 
	
	/**
	 * Instantiates a new creates the list.
	 */
	public CreateList()
	{
		super();
		viewCreateList();
	}
	
	/**
	 * View create list.
	 */
	public void viewCreateList(){
		
		MarketingDirectorGui.WelcomeLabel.setVisible(false);
		
		this.setSize( 1500,765 ); //set Frame size
		this.setLayout(null); //set layout
		this.setBackground(Color.LIGHT_GRAY); // set Background color to blue
		//----------------------------------------------
		backButton.setFont(new Font("Tahoma", Font.BOLD, 14));
		backButton.setBounds(1093, 90, 150, 23);   //set button's size and location
		add(backButton);          // add button to the panel
		
		DealNumberLabel = new JLabel();     //set new label
		DealNumberLabel.setBounds(new Rectangle(44, 52, 255, 41));//set label's  size and location
		DealNumberLabel.setText("Please Enter Deal Number:");   // set label name "User name:"
		DealNumberLabel.setFont(new Font("Arial", Font.BOLD, 20)); //set label font and size
		this.add(DealNumberLabel);
	
		DealNumberTextField = new JTextField(); //set User name field 
		DealNumberTextField.setFont(new Font("Tahoma", Font.PLAIN, 14));
		DealNumberTextField.setBounds(new Rectangle(108, 104, 127, 23)); //set field's  size and location
		this.add(DealNumberTextField);   // add the field to the panel

		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnNewButton.setVisible(false);
		btncreate.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btncreate.setVisible(false);
		btndiv.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btndiv.setVisible(false); 
		btnsend.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnsend.setVisible(false); 
		btnBack.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnBack.setVisible(false);
		EnterButton.setForeground(new Color(139, 0, 0));
		

		EnterButton.setFont(new Font("Tahoma", Font.BOLD, 14));
		EnterButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 
				 lst=new CreateListControl(DealNumberTextField.getText());
				 if(!lst.checkdetail()) //check if the field empty
				 {
					 confirmationLable.setVisible(true);
					 confirmationLable.setText("Please fill all the fields!!");
				 }
				 else 
				 { 
					 lst.submitCreateList();
					 textArea_2.setVisible(true);
					 confirmationLable.setText("");
					 EnterButton.setVisible(false);
					 btnNewButton.setVisible(true);
					 DealNumberLabel.setVisible(false);
						lblNewLabel.setText("Search Sector of deal:");
					 DealNumberTextField.setVisible(false);
				 } }});
		
		
		
				btnNewButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							 mtc=new MarketingTemplateControl();
							 final String totemp=textArea_2.getText();
			
							 String [] arr = new String [30]; 
							 arr=totemp.split(" "); 
							
							 mtc.SearchTemplate(arr[14]);
							 textArea_3.setVisible(true);
							 btnNewButton.setVisible(false);
								lblNewLabel.setText("Find Customers by sector:");
							 btnBack.setVisible(true);
							 btncreate.setVisible(true);
							 scrollPane.setVisible(true);
						}});		
						btnNewButton.setBounds(302, 496,150, 23);
							add( btnNewButton);
						 
						btncreate.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent arg0) { 
								tolist=textArea_3.getText();
								 textArea_2.setVisible(false);
								 textArea_3.setVisible(false);
								 btnNewButton.setVisible(false);
								 //---------------------------
								 String[] temp,temp2,temp3;
								 temp=tolist.split("Free text: ");
								 liarr=temp[0].split(" ");
								 //-------------------------
								 temp2=temp[1].split("Crokery tools Permission: ");
								temp3=temp2[1].split("Picnic tools Permission: ");
								//----------------------
								liarr=tolist.split(" ");
								cc=new CustomerControl();
								cc.searchBySector(liarr,temp3[0],temp3[1]);
								//----------------------------
								textArea_4.setVisible(true);
								btnBack.setVisible(false);
								btncreate.setVisible(false);
								lblNewLabel.setText("Divide List by number of saleman:");
								btndiv.setVisible(true); 
								}});			
		btncreate.setBounds(302, 496,150, 23);
		 add(btncreate);		
		EnterButton.setBounds(62,153, 85, 23);    
		add(EnterButton);                
		
		
		
		/**
		 * divide the list by the number of salesman
		 */
		
		 btndiv.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {  
			lst.conutSalesmen();
			textArea_1.setVisible(true);
			btndiv.setVisible(false); 
			lblNewLabel.setText("Send to salesmen:");
			 btnsend.setVisible(true);
			
		}});
		 btndiv.setBounds(302, 496,150, 23);
		 add(btndiv);
		 
		 
		/**
		 * Send the lists to the salesman
		 */
		 
		 
		 btnsend.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			todivide = textArea_1.getText();
			textArea_2.setVisible(false);
			textArea_3.setVisible(false);
			btnsend.setVisible(false);
			 JOptionPane.showMessageDialog(null,"The lists were sent to the salesmen");
			 lblNewLabel.setText("");
		}});
		 btnsend.setBounds(302, 496,150, 23);
		 add(btnsend);
				 
			confirmationLable = new JLabel("\n");   //set label for result
			confirmationLable.setForeground(new Color(204, 0, 51));
			confirmationLable.setFont(new Font("Tahoma", Font.BOLD, 14));
			confirmationLable.setBounds(405, 609, 589, 41);  //set label size
			add(confirmationLable);              // add the label to the frame
			
			
	/**
	 * back to the option enter deal id
	 */
			
			btnBack.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
						 EnterButton.setVisible(true);
						 btnNewButton.setVisible(false);
						  btncreate.setVisible(false);
						  btndiv.setVisible(false);
						  btnsend.setVisible(false);
						 textArea_1.setVisible(false);
						 textArea_2.setVisible(false);
						 textArea_3.setVisible(false);
						 textArea_4.setVisible(false);
						 DealNumberTextField.setVisible(true);
						 DealNumberTextField.setText("");
						 DealNumberLabel.setVisible(true);
						 DealNumberLabel.setText("Please Enter Deal Number:");
						 lblNewLabel.setText(" ");
				}});
			
			textArea_3 = new JTextArea();
			textArea_3.setBackground(Color.LIGHT_GRAY);
			textArea_3.setForeground(Color.DARK_GRAY);
			textArea_3.setFont(new Font("Tahoma", Font.BOLD, 14));
			textArea_3.setEditable(false);
			textArea_3.setBounds(44, 233, 356, 252);
			add(textArea_3);
			btnBack.setBounds(671, 187, 89, 23);
			add(btnBack);
			
			textArea_2 = new JTextArea();
			textArea_2.setBackground(Color.LIGHT_GRAY);
			textArea_2.setForeground(Color.DARK_GRAY);
			textArea_2.setFont(new Font("Tahoma", Font.BOLD, 14));
			textArea_2.setEditable(false);
			textArea_2.setBounds(44, 233, 356, 241);
			add(textArea_2);
						
			scrollPane = new JScrollPane();
			scrollPane.setBounds(476, 221, 300, 264);
			add(scrollPane);
						
			textArea_4 = new JTextArea();
			scrollPane.setViewportView(textArea_4);
			textArea_4.setEditable(false);
			textArea_4.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblNewLabel.setForeground(new Color(128, 0, 128));
						
					
			lblNewLabel.setFont(new Font("Arial", Font.BOLD, 16));
			lblNewLabel.setBackground(new Color(0, 0, 0));
			lblNewLabel.setBounds(10, 474, 289, 66);
			add(lblNewLabel);
			
			textArea_1 = new JTextArea();
			textArea_1.setFont(new Font("Tahoma", Font.BOLD, 14));
			textArea_1.setEditable(false);
			textArea_1.setBounds(985, 150, 133, 166);
		
		
			confirmationLable.setVisible(false);
	}
}
